//
//  XHRMedicalRecordViewController.m
//  快医
//
//  Created by 胥鸿儒 on 16/7/6.
//
//

#import "XHRMedicalRecordViewController.h"
#import "XHRMedicalRecordTopView.h"
#import "XHRMedicalRecordItem.h"
#import "XHRMedicalRecordCell.h"
#import "XHRAddMedicalRecordViewController.h"
@interface XHRMedicalRecordViewController ()<UITableViewDelegate,UITableViewDataSource>

/**顶部用户信息视图*/
@property(nonatomic,weak)XHRMedicalRecordTopView *topView;

/**底部button*/
@property(nonatomic,weak)UIButton *bottomButton;

/**tableView*/
@property(nonatomic,weak)UITableView *tableView;
/**病历数据数组*/
@property(nonatomic,strong)NSMutableArray <XHRMedicalRecordItem *>*dataArray;
@end

@implementation XHRMedicalRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSettings];
}
- (void)initSettings
{
    self.view.backgroundColor = [UIColor whiteColor];
}
#pragma mark -
#pragma mark - 监听按钮的点击
- (void)didClickAddRecordButton:(UIButton *)sender
{
    XHRAddMedicalRecordViewController *addRecordViewController = [[XHRAddMedicalRecordViewController alloc]init];
    addRecordViewController.navigationItem.title = sender.currentTitle;
    __weak typeof(self)weakSelf = self;
    addRecordViewController.addmedicalViewControllerBlock = ^(XHRMedicalRecordItem *medicalRecordItem){
        [weakSelf.dataArray insertObject:medicalRecordItem atIndex:0];
        [weakSelf.tableView reloadData];
        
        [NSKeyedArchiver archiveRootObject:weakSelf.dataArray  toFile:XHRMedicalRecordPath];
        
    };
    [self.navigationController pushViewController:addRecordViewController animated:YES];
}
#pragma mark -
#pragma mark - 布局子控件
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    [self.topView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.offset(0);
        make.height.equalTo(XHRScreenHeight * 0.28);
    }];
    [self.bottomButton makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(20);
        make.bottom.trailing.offset(-20);
        make.height.equalTo(40);
    }];
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.offset(0);
        make.top.equalTo(self.topView.bottom);
        make.bottom.equalTo(self.bottomButton.top);
    }];
}
#pragma mark -
#pragma mark - tableView dataSoruce
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XHRMedicalRecordCell *cell = [XHRMedicalRecordCell medicalRecordCellWithTabelView:tableView];
    
    cell.medicalRecordItem = self.dataArray[indexPath.row];
    return cell;
}
#pragma mark -
#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.dataArray[indexPath.row].cellHeight;
}
#pragma mark -
#pragma mark - 重写setter方法
- (void)setMedicalRecordItem:(XHRMedicalRecordItem *)medicalRecordItem
{
    _medicalRecordItem = medicalRecordItem;
    [self.dataArray insertObject:medicalRecordItem atIndex:0];
    [self.tableView reloadData];
}
#pragma mark -
#pragma mark - 懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]init];
        [self.view addSubview:tableView];
        tableView.tableFooterView = [[UIView alloc]init];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.dataSource = self;
        tableView.delegate = self;
        _tableView = tableView;
    }
    return _tableView;
}
- (XHRMedicalRecordTopView *)topView
{
    if (!_topView) {
        XHRMedicalRecordTopView *topView = [[XHRMedicalRecordTopView alloc]init];
        [self.view addSubview:topView];
        _topView = topView;
    }
    return _topView;
}
- (UIButton *)bottomButton
{
    if (!_bottomButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitle:@"添加病历" forState:UIControlStateNormal];
        button.backgroundColor = XHRColorWithRGB(25, 170, 187);
        [button addTarget:self action:@selector(didClickAddRecordButton:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
        [self.view addSubview:button];
        _bottomButton = button;
    }
    return _bottomButton;
}
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSKeyedUnarchiver unarchiveObjectWithFile:XHRMedicalRecordPath];
        if (!_dataArray) {
            _dataArray = [NSMutableArray array];
        }
        
    }
    return _dataArray;
}
@end
